package com.gc.web.controller.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.date.DateUtil;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import com.gc.framework.util.ShiroUtils;
import com.gc.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.ElevatorBrand;
import com.gc.business.service.IElevatorBrandService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 电梯品牌 控制层
 * 
 * @author 刘锦腾
 * @date 2020-12-08
 */
@Slf4j
@Controller
@RequestMapping("/business/elevatorBrand")
public class ElevatorBrandController extends BaseController {
    private String prefix = "business/elevatorBrand";
	
	private final IElevatorBrandService elevatorBrandService;

	@Autowired
	private ISysUserService userService;

	@Autowired
	public ElevatorBrandController(IElevatorBrandService elevatorBrandService) {
		this.elevatorBrandService = elevatorBrandService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:elevatorBrand:view")
	@GetMapping
	public String elevatorBrand(){
	    return prefix + "/elevatorBrand";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{brandId}")
	public String edit(@PathVariable("brandId") Long brandId, ModelMap mmap){
		ElevatorBrand elevatorBrand = elevatorBrandService.getById(brandId);
		mmap.put("elevatorBrand", elevatorBrand);
		return prefix + "/edit";
	}
	
	/**
	 * 查询电梯品牌列表
	 */
	@RequiresPermissions("business:elevatorBrand:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(ElevatorBrand elevatorBrand){
		Long attrId = getAttrId();
		if(userService.ifCompanyRole(attrId)){
			elevatorBrand.setCompanyId(attrId);
			startPage();
			List<ElevatorBrand> list = elevatorBrandService.list(elevatorBrand);
			return Result.success(getDataTable(list));
		}else{
			return Result.success(getDataTable(new ArrayList<ElevatorBrand>()));
		}
	}

	/**
	 * 新增保存电梯品牌
	 */
	@RequiresPermissions("business:elevatorBrand:add")
	@Log(title = "电梯品牌", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(ElevatorBrand elevatorBrand){
		elevatorBrand.setCompanyId(getAttrId());
		elevatorBrand.setCreateBy(ShiroUtils.getLoginName());
		elevatorBrand.setCreateTime(DateUtil.date());
		elevatorBrandService.save(elevatorBrand);
		return Result.success();
	}

	/**
	 * 修改保存电梯品牌
	 */
	@RequiresPermissions("business:elevatorBrand:edit")
	@Log(title = "电梯品牌", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(ElevatorBrand elevatorBrand){
		elevatorBrand.setUpdateBy(ShiroUtils.getLoginName());
		elevatorBrand.setUpdateTime(DateUtil.date());
		elevatorBrandService.updateById(elevatorBrand);
		return Result.success();
	}
	
	/**
	 * 删除电梯品牌
	 */
	@RequiresPermissions("business:elevatorBrand:remove")
	@Log(title = "电梯品牌", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		elevatorBrandService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出电梯品牌列表
	 */
	@RequiresPermissions("business:elevatorBrand:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(ElevatorBrand elevatorBrand){
		List<ElevatorBrand> list = elevatorBrandService.list(elevatorBrand);
		ExcelUtil<ElevatorBrand> util = new ExcelUtil<>(ElevatorBrand.class);
		util.exportExcel(list, "elevatorBrand");
		return Result.success();
	}
}
